﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZG_member.BLL;
using System.Configuration;

namespace ZG_member
{
    public partial class CardTemplateDesignMainForm : Form
    {
        public CardTemplateDesignMainForm()
        {
            InitializeComponent();
            height = this.Height;
            width = this.Width;
            this.Opacity = 0;
            //this.Height = 0;
            //this.Width = 0;
            skinEngine1.SkinFile = Application.StartupPath + ConfigurationManager.AppSettings["Skin"];
        }
        int height;
        int width;

        private void btnAdd_Click(object sender, EventArgs e)
        {
            CardTemplateForm cm = new CardTemplateForm(this);
            cm.ShowDialog();


        }



        private void CardTemplateDesignMainForm_Load(object sender, EventArgs e)
        {
            this.timer1.Start();
            this.label1.ForeColor = Color.Red;
            cardTemplates card = new cardTemplates();
            DataTable dt = card.GetList("").Tables[0];
            cardTemplatesGdv.DataSource = dt;
            cardTemplatesGdv.AllowUserToAddRows = false;
            dgvcontent();
        }

        private void btnDel_Click(object sender, EventArgs e)
        {


            int id = int.Parse(cardTemplatesGdv.CurrentRow.Cells["id"].Value.ToString());
            consumeSolution cs = new consumeSolution();
            List<Model.consumeSolution> list = cs.GetModelList(" cardTemplateId = " + id);
            if (list.Count > 0)
            {
                MessageBox.Show("该模板正在被使用，请逐级删除！");
                return;
            }
            cardTemplates card = new cardTemplates();
            if (card.Delete(id))
            {
                CardTemplateDesignMainForm_Load(sender, e);
            }
        }

        private void btnaddadvcontent_Click(object sender, EventArgs e)
        {
            advInfo info = new advInfo();
            Model.advInfo infoModel = new Model.advInfo();
            infoModel.advContent = txtContent.Text;
            int row = info.Add(infoModel);
            if (row > 0)
            {
                dgvcontent();
            }
            else
            {
                MessageBox.Show("系统发生错误，信息添加失败!");
            }
        }
        private void dgvcontent()
        {
            advInfo info = new advInfo();
            DataTable dt = info.GetList("").Tables[0];
            dgvadvinfo.DataSource = dt;
            dgvadvinfo.AllowUserToAddRows = false;
        }

        private void btndeladvcontent_Click(object sender, EventArgs e)
        {
            if (dgvadvinfo.Rows.Count > 0)
            {
                advInfo info = new advInfo();
                if (info.Delete(int.Parse(dgvadvinfo.CurrentRow.Cells["id1"].Value.ToString())))
                {
                    dgvcontent();
                }
            }
        }

        private void Print_Click(object sender, EventArgs e)
        {
            int id = int.Parse(cardTemplatesGdv.CurrentRow.Cells["id"].Value.ToString());
            cardTemplates card = new cardTemplates();
            var md = card.GetModel(id);
            CardTemplatePrint.temp = md.template;
            CardTemplatePrint ctp = new CardTemplatePrint();
            ctp.Show();
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            this.Top = this.Top + 10;
            this.Opacity = this.Opacity - 0.06;
            if (Opacity <= 0)
            {
                this.timer2.Stop();
                this.Close();
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Left += 2;
            this.Opacity += 0.1;
            if (Opacity == 1)
            {
                this.timer1.Stop();

            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.timer2.Start();
        }
        int flag = 0;
        private void CardTemplateDesignMainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (flag == 0)
            {
                e.Cancel = true;
                flag = 1;
                this.timer2.Start();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            cardTemplates ct = new cardTemplates();
            ZG_member.Model.cardTemplates ctmode = new ZG_member.Model.cardTemplates();
            int id = Convert.ToInt32(this.cardTemplatesGdv.CurrentRow.Cells["id"].Value);
            ctmode = ct.GetModel(id);
            CardTemplateForm cf = new CardTemplateForm(this);
            foreach (Label item in cf.pictureBox1.Controls)
            {
                if (item.Name == "templateString3")
                {
                    item.Text = ctmode.template;
                }
            }
            //cf.templateString.Text = ctmode.template;
            string[] tempSZ = ctmode.template.Split('\n');
            for (int i = 0; i < tempSZ.Length; i++)
            {
                cf.alreadyAddItem.Items.Add(tempSZ[i]);
            }
            cf.templateName.Text = (this.cardTemplatesGdv.CurrentRow.Cells["template"].Value).ToString();
            cf.Text = "修改模板";
            CardTemplateForm.ids = id;
            cf.Show();
        }

        private void cardTemplatesGdv_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            cardTemplates ct = new cardTemplates();
            ZG_member.Model.cardTemplates ctmode = new ZG_member.Model.cardTemplates();
            int id = Convert.ToInt32(this.cardTemplatesGdv.CurrentRow.Cells["id"].Value);
            ctmode = ct.GetModel(id);
            CardTemplateForm cf = new CardTemplateForm(this);
            foreach (Label item in cf.pictureBox1.Controls)
            {
                if (item.Name == "templateString3")
                {
                    item.Text = ctmode.template;
                }
            }
            //cf.templateString.Text = ctmode.template;
            string[] tempSZ = ctmode.template.Split('\n');
            for (int i = 0; i < tempSZ.Length; i++)
            {
                cf.alreadyAddItem.Items.Add(tempSZ[i]);
            }
            cf.templateName.Text = (this.cardTemplatesGdv.CurrentRow.Cells["template"].Value).ToString();
            cf.Text = "修改模板";
            CardTemplateForm.ids = id;
            cf.Show();
        }
    }
}
